package cn.xutongxue.interview.es.entity;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import cn.xutongxue.interview.entity.Question;
import cn.xutongxue.interview.entity.vo.QuestionVo;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.github.classgraph.json.Id;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Document(indexName = "question")
@Data
public class QuestionEsEntity implements Serializable {

    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final long serialVersionUID = 7649810729781961966L;

    @Id
    private Long id;

    private String title;

    private String content;

    private String answer;

    private List<String> tags;

    private Long userId;

    @Field(type = FieldType.Date, format = {}, pattern = DATE_TIME_PATTERN)
    private Date createTime;

    @Field(type = FieldType.Date, format = {}, pattern = DATE_TIME_PATTERN)
    private Date updateTime;

    private Integer isDelete;

    public static QuestionEsEntity objToEsEntity(Question question) {
        if (question == null) {
            return null;
        }
        QuestionEsEntity esEntity = new QuestionEsEntity();
        BeanUtil.copyProperties(question, esEntity);
        String tagsStr = question.getTags();
        if (StringUtils.isNotBlank(tagsStr)) {
            esEntity.setTags(JSONUtil.toList(tagsStr, String.class));
        }
        return esEntity;
    }

    public static Question esEntityToObj(QuestionEsEntity esEntity) {
        if (esEntity == null) {
            return null;
        }
        Question question = new Question();
        BeanUtil.copyProperties(esEntity, question);
        List<String> tagList = esEntity.getTags();
        if (CollUtil.isNotEmpty(tagList)) {
            question.setTags(JSONUtil.toJsonStr(tagList));
        }
        return question;
    }
}
